# -*- coding: utf-8 -*-

from odoo import models, fields, api
from odoo.addons.weixin.api.weixinapi import WeiXinApi
import logging

_loggle = logging.getLogger(__name__)


class weixin(models.Model):
    _name = 'weixin.user'

    partner_id = fields.Many2one('res.partner', string=u'客户')
    user_id = fields.Many2one('res.users', string=u'用户')
    open_id = fields.Char('openid')
    name = fields.Char(u'昵称')
    headimg = fields.Char(u'头像')
    sex = fields.Selection([
        ('male', u'男'),
        ('female', u'女')
    ], u'性别')
    status = fields.Boolean(u'状态')
    subscribetime = fields.Datetime(u'关注时间')
    unsubcribetime = fields.Datetime(u'取关时间')
    usestatus = fields.Selection([
        ('male', u'男'),
        ('female', u'女')
    ], u'性别')
    freezetime = fields.Datetime(u'冻结时间')
    unfreezetime = fields.Datetime(u'取消冻结')

    language = fields.Char(u'语言')
    city = fields.Char(u'城市')
    province = fields.Char(u'省')
    country = fields.Char(u'国家')
    unionid = fields.Char(u'unionid')
    groupid = fields.Char(u'groupId')
    tagidlist = fields.Text(u'tagidList')
    bindstatus = fields.Selection(
        [
            ('10', u'登录绑定'),
            ('20', u'手机验证')
        ], u'绑定标志')
    scene_code = fields.Char(u'scene')

    @api.model
    def add_weixin_user(self, weixinAuthData, mpUser):

        try:
            l_weixin_user = self.env["weixin.user"].search([("open_id", "=", mpUser["openid"])])

            if l_weixin_user:
                return {"err_code": 0, "firstFlag": "N"}
            else:
                weixin_user = {
                    "open_id": mpUser["openid"],
                    "session_key": weixinAuthData["weixinAuthData"],
                    "bindstatus": '10'
                }
                self.env["weixin.user"].create(weixin_user)
                return {"err_code": 0, "firstFlag": "Y"}

        except Exception as ex:
            return {"err_code": 0, "firstFlag": "Y"}

    @api.model
    # service_no,服务单编号
    # service_datetime,服务时间
    # service_item,服务项目
    # evaluate_help，评价说明
    def send_service_evaluate_notice(self, open_id, service_no, service_datetime, service_item, evaluate_help):
        try:
            _loggle.info(open_id)
            weixinApi = WeiXinApi()
            # open_id=""
            template_id = self.env['ir.config_parameter'].get_param('weixin_service_template')
            form_id = "wx201801241600432cc6ac9e330412344455"
            page = ""
            message = {
                "keyword1": {
                    "value": service_no
                },
                "keyword2": {
                    "value": service_datetime
                },
                "keyword3": {
                    "value": service_item
                },
                "keyword4": {
                    "value": evaluate_help
                }
            }
            emphasis_keyword = "keyword1.DATA"
            result = weixinApi.sendTemplateMessage(open_id, template_id, form_id, page, message, emphasis_keyword)

            return result
        except Exception as ex:
            return {"err_code": 1, "err_msg": ex.message}
